<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\shiyao;

use app\common\model\shiyao\ProjectPayCategory as ProjectPayCategoryModel;
use app\common\service\store\User as UserService;
use think\facade\Db;

/**
 * 申办方模型
 * Class StoreUser
 * @package app\store\model
 */
class ProjectPayCategory extends ProjectPayCategoryModel {

	public function getListAll() {
		// 查询模型
		$query = $this->getNewQuery();

		// 查询列表记录
		$list = $query
			->where('is_delete', '=', '0')
			->order(['sort' => 'asc', 'create_time' => 'desc'])
			->select()->toArray();

		return $list;
	}

	/**
	 * 获取用户列表
	 * @param array $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '']);

		$store_user = UserService::getLoginInfo();
		$store_user = $store_user['user'];
		!empty($store_user['company_id']) && $query->where('company_id', '=', $store_user['company_id']);

		// 关键词搜索
		!empty($params['search']) && $query->where('category_name', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query
			->where('is_delete', '=', '0')
			->order(['sort' => 'asc', 'create_time' => 'desc'])
			->select();

		return $list;
	}

	/**
	 * 新增记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data) {
		$store_user = UserService::getLoginInfo();
		$store_user = $store_user['user'];

		$data['company_id'] = $store_user['company_id'];
		$data['store_id'] = self::$storeId;

		// 事务处理
		$this->transaction(function () use ($data) {
			// 新增
			$this->save($data);

		});
		return true;
	}

	/**
	 * 更新记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data) {

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	/**
	 * 软删除
	 * @return false|int
	 */
	public function setDelete() {

		return $this->transaction(function () {
			// 删除对应的角色关系
			UserRole::deleteAll([['store_user_id', '=', (int) $this['store_user_id']]]);
			return $this->save(['is_delete' => 1]);
		});
	}

}
